<?php
namespace app\admin\controller;

use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\agent\IAgentAdminRecommendCache;
use app\common\controller\AdminBaseController;
use app\common\enum\AgentAdminStatus;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;

class AgentAdminRecommend extends AdminBaseController{

    /**
     * @功能：信贷经理推荐
     * @开发者：gys
     * @return string
     */
    public function agent_admin_recommend_list(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id");
        $agent_admin_recommend_location = IRequest::get("agent_admin_recommend_location");
        $phone = IRequest::get("phone");
        $real = IRequest::get("real");

        //设置查询条件
        $is_delete = IsDelete::No;
        $admin_status = AgentAdminStatus::normal;
        $where['car.is_delete'] = IsDelete::No;
        if(!empty($agent_admin_id)) $where['car.agent_admin_id'] = $agent_admin_id;
        if(!empty($agent_admin_recommend_location)) $where['car.agent_admin_recommend_location'] = $agent_admin_recommend_location;
        if(!empty($phone)) $where['ca.phone'] = ['like',"%{$phone}%"];
        if(!empty($real)) $where['ca.real'] = ['like',"%{$real}%"];

        //取得信贷经理数据
        $recommend_list = IDb::getInstance('agent_admin_recommend as car')
            ->setDbFiled("car.*,ca.avatar,ca.phone,ca.real,c.province_id,c.city_id")
            ->setDbJoin("agent_admin as ca", "ca.is_delete = {$is_delete} and ca.agent_admin_status={$admin_status} and ca.agent_admin_id=car.agent_admin_id")
            ->setDbJoin("agent as c","c.agent_id = ca.agent_id")
            ->setDbWhere($where)
            ->setDbOrder("car.order_by desc,car.agent_admin_id desc")
            ->pag();

        //页面赋值
        $this->assign("agent_admin_id",$agent_admin_id);
        $this->assign("agent_admin_recommend_location",$agent_admin_recommend_location);
        $this->assign("recommend_list",$recommend_list);
        $this->assign("phone",$phone);
        $this->assign("real",$real);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信贷经理推荐
     * @开发者：gys
     * @return string
     */
    public function agent_admin_recommend_del_list(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id");
        $agent_admin_recommend_location = IRequest::get("agent_admin_recommend_location");
        $phone = IRequest::get("phone");
        $real = IRequest::get("real");

        //设置查询条件
        $is_delete = IsDelete::No;
        $admin_status = AgentAdminStatus::normal;
        $where['car.is_delete'] = IsDelete::Yes;
        if(!empty($agent_admin_id)) $where['agent_admin_id'] = $agent_admin_id;
        if(!empty($agent_admin_recommend_location)) $where['agent_admin_recommend_location'] = $agent_admin_recommend_location;
        if(!empty($phone)) $where['ca.phone'] = ['like',"%{$phone}%"];
        if(!empty($real)) $where['ca.real'] = ['like',"%{$real}%"];

        //取得信贷经理数据
        $recommend_list = IDb::getInstance('agent_admin_recommend as car')
            ->setDbFiled("car.*,ca.avatar,ca.phone,ca.real,c.province_id,c.city_id")
            ->setDbJoin("agent_admin as ca", "ca.is_delete = {$is_delete} and ca.agent_admin_status={$admin_status} and ca.agent_admin_id=car.agent_admin_id")
            ->setDbJoin("agent as c","c.agent_id = ca.agent_id")
            ->setDbWhere($where)
            ->setDbOrder("car.order_by desc,car.agent_admin_id desc")
            ->pag();

        //页面赋值
        $this->assign("agent_admin_id",$agent_admin_id);
        $this->assign("agent_admin_recommend_location",$agent_admin_recommend_location);
        $this->assign("agent_admin_recommend_list",$recommend_list);
        $this->assign("phone",$phone);
        $this->assign("real",$real);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信贷经理推荐编辑
     * @开发者：gys
     * @return string
     */
    public function agent_admin_recommend_edit(){
        //接收参数
        $agent_admin_recommend_id = IRequest::get("agent_admin_recommend_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");

        //取得推荐详情
        $where = null;
        $where['agent_admin_recommend_id'] = $agent_admin_recommend_id;
        $agent_admin_recommend_info = IDb::getInstance('agent_admin_recommend')->setDbWhere($where)->row();

        //取得经理详情
        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_recommend_info['agent_admin_id']);

        //页面赋值
        $this->assign("agent_admin_recommend_id",$agent_admin_recommend_id);
        $this->assign("agent_admin_recommend_info",$agent_admin_recommend_info);
        $this->assign("agent_admin_info",$agent_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信贷经理推荐
     * @开发者：gys
     * @return string
     */
    public function agent_admin_recommend_submit(){
        //接收参数
        $agent_admin_recommend_id = IRequest::get("agent_admin_recommend_id",IRequest::NOT_EMPTY,"推荐编号不能为空！");
        $agent_admin_recommend_location = IRequest::get("agent_admin_recommend_location",IRequest::NOT_EMPTY,"信贷经理推荐位置编号不能为空！");
        $rate = IRequest::get("rate",IRequest::NUMBER,"评分不能为空！");
        $rate_num = IRequest::get("rate_num",IRequest::NUMBER,"评分人数不能为空！");
        $slogan = IRequest::get("slogan",IRequest::NOT_EMPTY,"服务不能为空！");
        $agent_admin_note = IRequest::get("agent_admin_note");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");

        //修改数据
        $data = null;
        $where['agent_admin_recommend_id'] = $agent_admin_recommend_id;
        $data['agent_admin_recommend_location'] = $agent_admin_recommend_location;
        $data['agent_admin_note'] = $agent_admin_note;
        $data['rate'] = $rate;
        $data['rate_num'] = $rate_num;
        $data['slogan'] = $slogan;
        $data['order_by'] = $order_by;
        $data['remark'] = $remark;
        $data['update_admin'] = get_login_admin_id();
        $data['update_time'] = get_date_time();
        $agent_admin_tag_upd = IDb::getInstance('agent_admin_recommend')->setDbWhere($where)->setDbData($data)->upd();
        if($agent_admin_tag_upd == false){
            error("推荐失败！");
        }

        //删除缓存
        IAgentAdminRecommendCache::deleteList();

        //画面跳转
        $this->success("操作成功！",Url("agent_admin_recommend_list"));
    }

    /**
     * @功能：信贷经理推荐删除
     * @开发者：gys
     * @return string
     */
    public function agent_admin_recommend_del(){
        //接收参数
        $agent_admin_recommend_id= IRequest::get("agent_admin_recommend_id",IRequest::NOT_EMPTY,"信贷经理推荐编号不能为空！");

        //修改管理员信息
        $where = null;
        $data = null;
        $where['agent_admin_recommend_id'] = $agent_admin_recommend_id;
        $data['is_delete'] = IsDelete::Yes;
        $data['update_admin'] = get_login_admin_id();
        $data['update_time'] = get_date_time();
        $agent_admin_recommend_upd = IDb::getInstance('agent_admin_recommend')->setDbWhere($where)->setDbData($data)->upd();
        if($agent_admin_recommend_upd === false){
            error("信贷经理推荐删除失败！");
        }

        //删除缓存
        IAgentAdminRecommendCache::deleteList();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：信贷经理推荐还原
     * @开发者：gys
     * @return string
     */
    public function agent_admin_recommend_recycle(){
        //接收参数
        $agent_admin_recommend_id= IRequest::get("agent_admin_recommend_id",IRequest::NOT_EMPTY,"信贷经理推荐编号不能为空！");

        //取得推荐的信贷经理
        $where = null;
        $where['agent_admin_recommend_id'] = $agent_admin_recommend_id;
        $agent_admin_recommend_info = IDb::getInstance('agent_admin_recommend')->setDbWhere($where)->row();
        if($agent_admin_recommend_info === false){
            error("信贷经理推荐还原失败！");
        }

        //取得信贷经理编号
        if(empty($agent_admin_recommend_info['agent_admin_id'])){
            error("信贷经理推荐还原失败！");
        }

        //取得信贷经理信息
        $agent_admin_info = IDb::getInstance('agent_admin')
            ->setDbFiled("agent_admin_id,agent_admin_status")
            ->setDbWhere(['agent_admin_id'=>$agent_admin_recommend_info['agent_admin_id']])
            ->row();

        //判断信贷经理状态
        if(empty($agent_admin_info['agent_admin_status']) || $agent_admin_info['agent_admin_status'] != AgentAdminStatus::normal){
            error("信贷经理已被删除不能推荐！");
        }

        //修改管理员信息
        $where = null;
        $data = null;
        $where['agent_admin_recommend_id'] = $agent_admin_recommend_id;
        $data['is_delete'] = IsDelete::No;
        $data['update_admin'] = get_login_admin_id();
        $data['update_time'] = get_date_time();
        $agent_admin_recommend_upd = IDb::getInstance('agent_admin_recommend')->setDbWhere($where)->setDbData($data)->upd();
        if($agent_admin_recommend_upd === false){
            error("信贷经理推荐还原失败！");
        }

        //删除缓存
        IAgentAdminRecommendCache::deleteList();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：取得信贷经理详情
     * @开发者：gys
     * @return string
     */
    public function agent_admin_recommend_info(){
        //接收参数
        $agent_admin_recommend_id= IRequest::get("agent_admin_recommend_id",IRequest::NOT_EMPTY,"信贷经理推荐编号不能为空！");

        //取得推荐信息
        $where = null;
        $where['agent_admin_recommend_id'] = $agent_admin_recommend_id;
        $agent_admin_recommend_info = IDb::getInstance('agent_admin_recommend')->setDbWhere($where)->row();
        if($agent_admin_recommend_info === false){
            error("取得信贷经理推荐信息失败！");
        }

        //取得信贷经理信息
        $agent_admin_info = IDb::getInstance('agent_admin')
            ->setDbFiled("agent_admin_id,phone,real,avatar")
            ->setDbWhere(['agent_admin_id'=>$agent_admin_recommend_info['agent_admin_id']])
            ->row();
        if($agent_admin_info === false){
            error("取得信贷经理信息失败！");
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("agent_admin_recommend_info",$agent_admin_recommend_info);
        $this->assign("agent_admin_info",$agent_admin_info);

        //渲染模板输出
        return view();
    }
}
